#region License
// WorkspaceViewModel.cs
// 
// Copyright (c) 2012 Xoqal.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace Shammateh.Win.UI.ViewModels
{
    using System;
    using System.Windows.Input;
    using Xoqal.Presentation.Commands;
    using Xoqal.Presentation.ViewModels;

    /// <summary>
    /// This ViewModelBase subclass requests to be removed 
    /// from the UI when its CloseCommand executes.
    /// </summary>
    public class WorkspaceViewModel<T> : ViewModelBase, IWorkspaceViewModel<T>
        where T : class, IViewModel
    {
        #region Fields

        private RelayCommand<object> closeCommand;
        private T contentViewModel;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="WorkspaceViewModel" /> class.
        /// </summary>
        /// <param name="contentViewModel">The content view model.</param>
        public WorkspaceViewModel(T contentViewModel)
        {
            this.ContentViewModel = contentViewModel;
            if (this.ContentViewModel is IRefreshableViewModel)
            {
                ((IRefreshableViewModel)this.ContentViewModel).RefreshCommand.Execute(null);
            }
        }

        #endregion

        /// <summary>
        /// Raised when this workspace should be removed from the UI.
        /// </summary>
        public event EventHandler RequestClose;

        #region Properties

        /// <summary>
        /// Gets or sets the content view model.
        /// </summary>
        /// <value>The content view model.</value>
        public T ContentViewModel
        {
            get 
            { 
                return this.contentViewModel;
            }

            set
            {
                if (value == this.contentViewModel)
                {
                    return;
                }

                this.contentViewModel = value;

                this.RaisePropertyChanged(() => this.ContentViewModel);
            }
        }

        /// <summary>
        /// Gets or sets the content view model.
        /// </summary>
        /// <value>
        /// The content view model.
        /// </value>
        IViewModel IContainerViewModel.ContentViewModel
        {
            get { return this.ContentViewModel; }
            set { this.ContentViewModel = (T)value; }
        }

        #endregion

        #region CloseCommand

        /// <summary>
        /// Returns the command that, when invoked, attempts
        /// to remove this workspace from the user interface.
        /// </summary>
        public ICommand CloseCommand
        {
            get
            {
                if (this.closeCommand == null)
                {
                    this.closeCommand = new RelayCommand<object>(param => this.OnRequestClose());
                }

                return this.closeCommand;
            }
        }

        #endregion

        #region RequestClose [event]

        private void OnRequestClose()
        {
            EventHandler handler = this.RequestClose;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }

        #endregion
    }
}
